//ajax functions





//diagnostic Test information
function editDiagnosticTestInformation (id) {
	var value1 = $("#"+id).find("#"+id+"1").val();
	var value2 = $("#"+id).find("#"+id+"2").val();
    
	$.post('../Ajax_Requests/updateDiagnosticTestInformation.php',
			{
				result1: value1,
				result2: value2,
				id: id
			},
			 
			function(data) {
				if (data.length > 0) {
					if (data === 'error') {
						$("#diagnosticInfoClick").html("Diagnostic Test Information - <span style='color: red;'>Error in Database Connection</span>")
					}
					else {
						$("#"+id).html("<td style='color: green;'>Updated!</td><td style='color: green;'>Updated!</td><td style='color: green;'>Updated!</td><td style='color: green;'>Updated!</td><td style='color: green;'>Updated!</td>");
						$("#" + id).hide(1800);
					}
				}
			}
		);
}


//prescription information
function editPrescriptionTestInformation (id) {

	var presid = id.substring(14);

	var dosage = $("#"+id).find("#prescriptionDosage"+id.substring(14)).val();
	var startdate = $("#"+id).find("#prescriptionStartDate"+id.substring(14)).val();
	var enddate = $("#"+id).find("#prescriptionEndDate"+id.substring(14)).val();

	$.post('../Ajax_Requests/updatePrescriptionInformation.php',
			{
				id: presid,
				dosage: dosage,
				startdate: startdate,
				enddate:enddate
			},

			function(data) {
				if (data.length > 0) {
					if (data === "error")
						$("#prescriptionInfoClick").html("Prescription Information - <span style='color: red;'>Error in Database Connection</span>");
					else if (data === "missing") 
						$("#prescriptionInfoClick").html("Prescription Information - <span style='color: red;'>Missing Field Value</span>");
					else {
						$("#"+id).html("<td style='color: green;'>Updated!</td><td style='color: green;'>Updated!</td><td style='color: green;'>Updated!</td><td style='color: green;'>Updated!</td><td style='color: green;'>Updated!</td>");
						$("#" + id).hide(1800);
					}
				}
			}
	);
}


//psychological test information
function editPsychologicalTestInformation (id) {

	var psyid = id.substring(15);
	var anxiety = $("#"+id).find("#psyqnum1Sel"+psyid).val();
	var depression = $("#"+id).find("#psyqnum2Sel"+psyid).val();
	var cognition = $("#"+id).find("#psyqnum3Sel"+psyid).val();
	var global = $("#"+id).find("#psyGlobalSel"+psyid).val();
	var date = $("#"+id).find("#psyRecordDate"+psyid).val();

	$.post('../Ajax_Requests/updatePsychologicalTestInformation.php',
			{
				id: psyid,
				anxiety: anxiety,
				depression: depression,
				cognition: cognition,
				global: global,
				date: date
			},

			function(data) {
				if (data.length > 0) {
					if (data === "error")
						$("#psychologicalTestClick").html("Psychological Test Information - <span style='color: red;'>Error in Database Connection</span>");
					else if (data === "missing") 
						$("#psychologicalTestClick").html("Psychological Test Information - <span style='color: red;'>Missing Field Value</span>");
					else {
						$("#"+id).html("<td style='color: green;'>Updated!</td><td style='color: green;'>Updated!</td><td style='color: green;'>Updated!</td><td style='color: green;'>Updated!</td><td style='color: green;'>Updated!</td>");
						$("#" + id).hide(1800);
					}
				}
			}
	);
} 


function editPhysicalSyptomInformation(id) {
	
	var selects = $("#" + id + " select"); // all select elements
	var reference = id.substring(17); //get row number (id from database table)
	var select_ids = $.map(selects, function(value, index) { //return an array of select ids and their associated values
				var select_id = value.id;
				var number = select_id.substring(17);
				var option = $("#" + select_id + " option:selected").val();
				return new Array(new Array(number, option));
		}
	);
	var dateVal = $("#physRecordDate" + reference).val(); //get value of date entry
	//var select_id = $("#" + id + " select").attr('id');
	$.post('../Ajax_Requests/updatePhysicalSymptoms.php',
			{
				id: reference,
				answer: select_ids,
				record_date: new Array("physRecordDate" + reference, dateVal)
			},

			function (data) {
				if (data.length > 0) {					
					if (data == "fail") 
						alert("WARNING: Error in POST, information NOT stored in database");
					else if (data == "success") {
						var nodes = $("#" + id).children();
						var html = "";
						var i;
						for (i = 0; i < nodes.length; i++)
							html += "<td style='color: green;'>Updated!</td>";
						$("#"+id).html(html);
						$("#" + id).hide(1800);
					} else {
						alert("other");
						console.log(data);
					}
				} else
					alert("WARNING: Error in POST, information NOT stored in database");
			}
	);
}



function editNeuroPsychologicalSyptomInformation(id) {

	var selects = $("#" + id + " " + "select"); // all select elements
	var reference = id.substring(20); //get row number (id from database table)
	var select_ids = $.map(selects, function(value, index) { //return an array of select ids and their associated values
				var select_id = value.id;
				var number = select_id.substring(19);
				var option = $("#" + select_id + " option:selected").val();
				return new Array(new Array(number, option));
		}
	);
	
	var dateVal = $("#neuroPsychRecordDate" + reference).val(); //get value of date entry
	//var select_id = $("#" + id + " select").attr('id');
	$.post('../Ajax_Requests/updateNeuroPsychologicalSymptoms.php',
			{
				id: reference,
				answer: select_ids,
				record_date: new Array("physRecordDate" + reference, dateVal)
			},

			function (data) {
				if (data.length > 0) {					
					if (data == "fail") 
						alert("WARNING: Error, information NOT stored in database");
					else if (data == "success") {
						var nodes = $("#" + id).children();
						var html = "";
						var i;
						for (i = 0; i < nodes.length; i++)
							html += "<td style='color: green;'>Updated!</td>";
						$("#"+id).html(html);
						$("#" + id).hide(1800);
					} else {
						alert("other");
						console.log(data);
					}
				} else
					alert("WARNING: Error, information NOT stored in database");
			}
	);
}


function editICDInformation(id) {

	var selects = $("#" + id + " select"); // all select elements
	var icd_nine_entry_id = id.substring(3); //get row number (id from database table)
	var icd_code_id = $.map(selects, function(value, index) { //return an array of select ids and their associated values
				return $("#" + value.id + " option:selected").val();
		}
	);

	var dateVal = $("#icdDate" + icd_nine_entry_id).val(); //get value of date entry

	$.post('../Ajax_Requests/updateICDInformation.php',
			{
				icd_nine_entry_id: icd_nine_entry_id,
				icd_code_id: icd_code_id,
				record_date: dateVal
			},

			function (data) {
				if (data.length > 0) {
					if (data == "fail") 
						alert("WARNING: Error in POST, information NOT stored in database");
					else if (data == "success") {
						var nodes = $("#" + id).children();
						var html = "";
						var i;
						for (i = 0; i < nodes.length; i++)
							html += "<td style='color: green;'>Updated!</td>";
						$("#"+id).html(html);
						$("#" + id).hide(1800);
					} else {
						alert("other");
						//console.log(data);
					}
				} else
					alert("WARNING: Error in POST, information NOT stored in database");
			}
	);
}


function checkPatientId(id) {

	$.post('../Ajax_Requests/generatePatientIds.php',
			{
				result: "hello"
			},
			 
			function(data) {
				if (data.length > 0) {
					if (data === 'error') {
						$("#diagnosticInfoClick").html("Diagnostic Test Information - <span style='color: red;'>Error in Database Connection</span>")
					}
					else {
						//$("#"+id).html("<td style='color: green;'>Updated!</td><td style='color: green;'>Updated!</td><td style='color: green;'>Updated!</td><td style='color: green;'>Updated!</td><td style='color: green;'>Updated!</td>");
						//$("#" + id).hide(1800);
						alert(id);
					}
				}
			}
		);
}